/**
 * 复活界面
 */
class ReliveUI extends UIBase{
    private img_mask:eui.Image;
    private lb_time:eui.BitmapLabel;
    private btn_relive:BaseButton;
    private lb_jump:eui.Label;
    private time_group:eui.Group;

    private shape:egret.Shape;
    private curNum:number = 9;
    private Timer:number = 0;
    private angle:number = -90;
    private isJump:boolean = false;

    private lb_cost:eui.Label;
    private group_show:eui.Group;
    private img_gou:eui.Image;
    private lb_coin:eui.Label;
    private recommendItem:RecommendUI;

    private curCost:number;
    constructor()
    {
        super('ReliveView');
        SoundManager.instance().playSound('fail_mp3', 1);
        UIManager.instance().hideLamp();
    }

    public getNeedReliveCoin():number
    {
        let curCustoms = GameManager.instance.curCustoms;
        let curCustom_10 = Math.ceil(curCustoms / 10); // 除10后向上取整
        let total_coin = 0;
        for(let i=1;i<=curCustom_10;i++)
        {
            let coin = (i * 5 + 20) * 10;
            total_coin += coin;
        }
        
        return Math.floor(total_coin * 0.9);
    }

    public init()
    {
        super.init(true);
        this.lb_time.text = this.curNum + '';
        this.lb_coin.text = RoleCache.instance().myMoney;
        if(!RoleCache.instance().reviewStatus)
        {
            this.lb_jump.touchEnabled = false;
            this.lb_jump.alpha = 0;
            let self = this;
            egret.setTimeout(()=>{
                self.lb_jump.touchEnabled = true;
                egret.Tween.get(self.lb_jump).to({
                alpha:1
            }, 1000)},this,1000)
        }
        this.addMask();
        let myCoin = RoleCache.instance().myMoney;

        let curCustomsIndex = GameManager.instance.curCustomsIndex;
        this.curCost = this.getNeedReliveCoin();

        this.lb_cost.text = this.curCost + '';
        if(myCoin < this.curCost)
        {
            this.lb_cost.textColor = 0xc8c8c8;
        }
    }

    public addMask()
    {
        let img_mask = this.img_mask;
        this.shape = new egret.Shape();
        this.img_mask.mask = this.shape;
        this.time_group.addChild(this.shape);
    }

    public addEvent()
    {
        this.btn_relive.addTapHandler(this.relive, this);
        this.lb_jump.addEventListener(egret.TouchEvent.TOUCH_TAP, this.jump, this);
        GameUtil.addEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
        this.group_show.addEventListener(egret.TouchEvent.TOUCH_TAP,this.boxHandle,this);

        egret.startTick(this.update, this);
    }

    protected boxHandle()
    {
        if(!this.img_gou.visible)
        {
            let myCoin = RoleCache.instance().myMoney;
            if(myCoin < this.curCost)
            {
                MessageUtil.tips('金币不足，不支持此项复活');
                return ;
            }
        }
        this.img_gou.visible = !this.img_gou.visible;
        let img_btn = this.btn_relive.getChildByName('relive_img') as eui.Image;
        img_btn.source = this.img_gou.visible ? 'btn_new_relive_png': 'btn_relive1_png';
    }

    protected shareSuccess(e:BaseEvent)
    {
        let type = e.param['type'];
        if(type == ShareId.relive)
        {
            this.alive();
        }
    }

    protected update()
    {
        this.Timer++;
        this.angle+=0.6;
        this.changeGraphics(this.angle);
        this.angle %= 360;
        if(this.Timer % 60==0)
        {
            if(this.curNum <= 0)
            {
                this.jump();
                return;
            }
            this.Timer = 0;
            this.curNum--;
            this.lb_time.text =  this.curNum + ''
        }
        return true;
    }

    protected changeGraphics(angle) {
        if(!this.shape) return;
        this.shape.graphics.clear();
        let img_mask = this.img_mask;
        let r = img_mask.width / 2;
        this.shape.graphics.beginFill(0xff0000);
        
        this.shape.graphics.moveTo(img_mask.x+r, img_mask.y+r);
        this.shape.graphics.lineTo(img_mask.x+r, img_mask.y+r);
        this.shape.graphics.drawArc(img_mask.x+r, img_mask.y+r, r, -Math.PI / 2, angle * Math.PI / 180, true);
        this.shape.graphics.lineTo(img_mask.x+r, img_mask.y+r);
        this.shape.graphics.endFill();
    }

    protected relive()
    {
        // if(egret.Capabilities.os == "Windows PC")
        // {
        //    this.alive();
        // }
        // else
        // {
            // SharePrase.instance().requestOneAdvertisement({type:ShareId.relive});
            if(!this.img_gou.visible)
            {
                //复活视频
                egret.stopTick(this.update, this);
                let self = this;
                let Success_call = function()
                {  
                    self.alive();
                };
                let Error_call = function()
                {  
                    MessageUtil.tips('观看完整视频才能复活哦！');
                    egret.startTick(this.update, this);
                };
                WeChatgGameManager.instance().showVideoAd(GameConst.adList.relive_vedio, Success_call, Error_call, this);
                }
            else
            {
                //金币复活
                let myCoin = RoleCache.instance().myMoney;
                if(myCoin >= this.curCost)
                {
                    this.goldRelive(this.curCost);
                }
            }
            
        // }
    }

    protected goldRelive(money)
    {
        //金币复活接口
        let parma = {
            version:GameConst.version,
            cost:`1_${money}`
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warrioruser/resurrect' , StringUtil.jsonToHttpPostString(parma), (e)=>{
            let data = JSON.parse(e);
            if(data && data.msg == 'ok')
            {
                RoleCache.instance().myMoney = data.data.coin;
                // RoleCache.instance().myDiamond = data.data.diamond;
                GameUtil.dispatchEvent(EventConst.RESOURCE_CHANGE);
                this.alive();
            }
            else
            {
                MessageUtil.tips('未知错误！');
            }
        })
    }

    protected alive()
    {
        MessageUtil.tips('复活成功');
        this.isJump = false;
        let logic:GameUI = UIManager.instance().getLogicByName(UIConst.GAME_UI) as GameUI;
        if(logic)
        {
            logic.relive();
        }
        GameUtil.sendCntTOSever('video_num');
        this.remove(0);
    }

    protected jump()
    {
        this.isJump = true;
        this.remove(0);
    }

    public removeEvent()
    {
        egret.stopTick(this.update, this);
        this.btn_relive.removeTapHandler();
        this.lb_jump.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.jump, this);
        GameUtil.removeEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
        this.group_show.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.boxHandle,this);
    }

    public clear()
    {
        this.shape = null;
        this.curNum = null;
        this.Timer = null;
        this.angle = null;
        this.img_mask.mask = null;
        this.recommendItem.remove();
        this.recommendItem = null;
        super.clear();
        if(this.isJump)
        {
            this.isJump = null;
            let curCustoms = GameManager.instance.curCustoms;
            RankManager.instance().uploadUserScore(curCustoms,'score');
            let data = {
                isPop:true,
                effect:0
            }
            UIManager.instance().openUILogic(GameOverUI, data);
        }
        UIManager.instance().showLamp();
    }
}